package com.hujh.android.ui.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.hujh.android.MainActivity;
import com.hujh.android.R;

public class ExampleAppWidgetProvider extends AppWidgetProvider{

	
	//定义一个常量字符串，该常量用于命名Action
	private static final String UPDATE_ACTION = "hujh.UPDATE_APP_WIDGET";
		
	
	/**
	 * 在到达指定的更新时间之后或当用户向桌面添加app widget时会调用该方法
	 */
	public void onUpdate1(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
		for (int i = 0; i < appWidgetIds.length; i++) {
			System.out.println(appWidgetIds[i]);
			//创建一个Intent对象
			Intent intent = new Intent(context,MainActivity.class);
			//创建一个PendingIntent
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
			//为按钮绑定事件处理器
			//第一个参数用来指定被绑定处理器的控件的ID
			//第二个参数用来指定当事件发生时，哪个PendingIntent将会被执行
			remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
			//更新AppWidget
			//第一个参数用于指定被更新AppWidget的ID
			appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
		//创建一个Intent对象
		Intent intent = new Intent();
		//为Intent对象设置Action
		intent.setAction(UPDATE_ACTION); //自定义action
		//使用getBroadcast方法，得到一个PendingIntent对象，当该对象执行时，会发送一个广播
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent, 0);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
		remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
	}
	
	/**
	 * 当app widget初删除时调用
	 */
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		System.out.println("onDeleted");
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	/**
	 * 当最后一个widget被删除时调用
	 */
	public void onDisabled(Context context) {
		System.out.println("onDisabled");
		super.onDisabled(context);
	}
	
	
	/**
	 * 当第一个widget初创建时调用
	 */
	@Override
	public void onEnabled(Context context) {
		System.out.println("onEnabled");
		super.onEnabled(context);
	}
	
	/**
	 * 接收广播消息时调用
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("onReceive");
		/*super.onReceive(context, intent);
		String action = intent.getAction();
		if (UPDATE_ACTION.equals(action)) {
			System.out.println("onReceive--->" + UPDATE_ACTION);
		}*/
		
		String action = intent.getAction();
		if (UPDATE_ACTION.equals(action)) {
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.example_appwidget);
			remoteViews.setImageViewResource(R.id.imageId, R.drawable.ku);
			remoteViews.setTextViewText(R.id.widgetTextId, "test");
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			ComponentName componentName = new ComponentName(context,ExampleAppWidgetProvider.class);
			appWidgetManager.updateAppWidget(componentName, remoteViews);
		} else {
			super.onReceive(context, intent);
		}
	}

	
}
